
<!DOCTYPE html>
<html lang="">


<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content>
  

  
    <meta name="description" content="Go语言之fmt的使用">
  
  
  
  <link rel="icon" type="image/x-icon" href="/images/footer-logo.png">
  
  <title>Go语言之fmt的使用 [ 51AIOps 专注于运维自动化  微信： kaipython ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="//cdn.bootcss.com/pure/1.0.0/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
</head>


<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    
    <span class="title" style="text-transform:none">51AIOps 专注于运维自动化  微信： kaipython</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            
              <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
            
          
      
  </ul>
   
</nav>

  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner" style='margin-left:-68px!important'>
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        Go语言之fmt的使用
      </h1>
      <span>
        
        <time class="time" datetime="2019-10-28T16:00:00.000Z">
        2019-10-29
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
    </header>

    <div class="post-content">
      <h3 id="Go语言fmt-Printf使用指南"><a href="#Go语言fmt-Printf使用指南" class="headerlink" title="Go语言fmt.Printf使用指南"></a>Go语言fmt.Printf使用指南</h3><p>fmt标准库是我们在学习Go语言过程中接触最早最频繁的一个了，本文介绍了fmtb包的一些常用函数。</p>
<h3 id="fmt"><a href="#fmt" class="headerlink" title="fmt"></a>fmt</h3><p>fmt包实现了类似C语言printf和scanf的格式化I/O。主要分为向外输出内容和获取输入内容两大部分</p>
<h4 id="向外输出的函数"><a href="#向外输出的函数" class="headerlink" title="向外输出的函数"></a>向外输出的函数</h4><ol>
<li><code>Print</code></li>
</ol>
<blockquote>
<p><code>Print</code>系列函数会将内容输出到系统的标准输出，区别在于<code>Print</code>函数直接输出内容，<code>Printf</code>函数支持格式化输出字符串，<code>Println</code>函数会在输出内容的结尾添加一个换行符。</p>
</blockquote>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Print</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br><span class="line"><span class="function"><span class="title">func</span> <span class="title">Printf</span><span class="params">(format <span class="keyword">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br><span class="line"><span class="function"><span class="title">func</span> <span class="title">Println</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="params">(n <span class="keyword">int</span>, err error)</span></span></span><br></pre></td></tr></table></figure>

<p>简单的例子：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	fmt.Print(<span class="string">"在终端打印该信息。"</span>)</span><br><span class="line">	name := <span class="string">"szk"</span></span><br><span class="line">	fmt.Printf(<span class="string">"我是：%s\n"</span>, name)</span><br><span class="line">	fmt.Println(<span class="string">"在终端打印单独一行显示"</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><code>print</code></li>
</ol>
<p><code>Sprint</code>系列函数会把传入的数据生成并返回一个字符串。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Sprint</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="title">string</span></span></span><br><span class="line"><span class="function"><span class="title">func</span> <span class="title">Sprintf</span><span class="params">(format <span class="keyword">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="title">string</span></span></span><br><span class="line"><span class="function"><span class="title">func</span> <span class="title">Sprintln</span><span class="params">(a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="title">string</span></span></span><br></pre></td></tr></table></figure>

<p>简单的示例代码如下：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">s1 := fmt.Sprint(<span class="string">"szknb"</span>)</span><br><span class="line">name := <span class="string">"szknb"</span></span><br><span class="line">age := <span class="number">18</span></span><br><span class="line">s2 := fmt.Sprintf(<span class="string">"name:%s,age:%d"</span>, name, age)</span><br><span class="line">s3 := fmt.Sprintln(<span class="string">"szknb"</span>)</span><br><span class="line">fmt.Println(s1, s2, s3)</span><br></pre></td></tr></table></figure>

<ol start="3">
<li><code>Errorf</code></li>
</ol>
<p><code>Errorf</code>函数根据format参数生成格式化字符串并返回一个包含该字符串的错误。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Errorf</span><span class="params">(format <span class="keyword">string</span>, a ...<span class="keyword">interface</span>&#123;&#125;)</span> <span class="title">error</span></span></span><br></pre></td></tr></table></figure>

<p>Go1.13版本为<code>fmt.Errorf</code>函数新加了一个<code>%w</code>占位符用来生成一个可以包裹Error的Wrapping Error。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">e := errors.New(<span class="string">"原始错误e"</span>)</span><br><span class="line">w := fmt.Errorf(<span class="string">"Wrap了一个错误%w"</span>, e)</span><br></pre></td></tr></table></figure>

<h4 id="格式化占位符"><a href="#格式化占位符" class="headerlink" title="格式化占位符"></a>格式化占位符</h4><p><code>*printf</code>系列函数都支持format格式化参数，在这里我们按照占位符将被替换的变量类型划分，方便查询和记忆。</p>
<ol>
<li><h5 id="通用占位符"><a href="#通用占位符" class="headerlink" title="通用占位符"></a>通用占位符</h5></li>
</ol>
<table>
<thead>
<tr>
<th>占位符</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>%v</td>
<td>值的默认格式表示</td>
</tr>
<tr>
<td>%+v</td>
<td>类似%v，但输出结构体时会添加字段名</td>
</tr>
<tr>
<td>%#v</td>
<td>输出值和值的类型</td>
</tr>
<tr>
<td>%T</td>
<td>打印值的类型</td>
</tr>
<tr>
<td>%%</td>
<td>百分号</td>
</tr>
</tbody></table>
<p>代码如下</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">fmt.Printf(<span class="string">"%v\n"</span>, <span class="number">100</span>)  ## <span class="number">100</span></span><br><span class="line">fmt.Printf(<span class="string">"%v\n"</span>, <span class="literal">false</span>) ## <span class="literal">false</span></span><br><span class="line">o := <span class="keyword">struct</span>&#123; name <span class="keyword">string</span> &#125;&#123;<span class="string">"szk"</span>&#125; </span><br><span class="line">fmt.Printf(<span class="string">"%v\n"</span>, o)  ## &#123;szk&#125;</span><br><span class="line">fmt.Printf(<span class="string">"%#v\n"</span>, o) ## <span class="keyword">struct</span> &#123; name <span class="keyword">string</span> &#125;&#123;name:<span class="string">"szk"</span>&#125;</span><br><span class="line">fmt.Printf(<span class="string">"%T\n"</span>, o) ## <span class="keyword">struct</span> &#123; name <span class="keyword">string</span> &#125;</span><br><span class="line">fmt.Printf(<span class="string">"100%%\n"</span>) ## <span class="number">100</span>%</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>指针</li>
</ol>
<table>
<thead>
<tr>
<th>占位符</th>
<th><strong>说明</strong></th>
</tr>
</thead>
<tbody><tr>
<td>%p</td>
<td>表示为十六进制，并加上前导的0x</td>
</tr>
</tbody></table>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a := <span class="number">10</span></span><br><span class="line">fmt.Printf(<span class="string">"%p\n"</span>, &amp;a) <span class="comment">// 0xc000094000</span></span><br><span class="line">fmt.Printf(<span class="string">"%#p\n"</span>, &amp;a) <span class="comment">// c000094000</span></span><br></pre></td></tr></table></figure>

<h4 id="获取输入的函数"><a href="#获取输入的函数" class="headerlink" title="获取输入的函数"></a>获取输入的函数</h4><p>Go语言<code>fmt</code>包下有<code>fmt.Scan</code>、<code>fmt.Scanf</code>、<code>fmt.Scanln</code>三个函数，可以在程序运行过程中从标准输入获取用户的输入。</p>
<ol>
<li><code>fmt.Scan</code></li>
</ol>
<p>示例代码：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> (</span><br><span class="line">		name    <span class="keyword">string</span></span><br><span class="line">		age     <span class="keyword">int</span></span><br><span class="line">		married <span class="keyword">bool</span></span><br><span class="line">	)</span><br><span class="line">	fmt.Scan(&amp;name, &amp;age, &amp;married)</span><br><span class="line">	fmt.Printf(<span class="string">"扫描结果 name:%s age:%d married:%t \n"</span>, name, age, married)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>将上面的代码编译后在终端执行，在终端依次输入<code>szk</code>、<code>28</code>和<code>false</code>使用空格分隔。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ ./scan_demo </span><br><span class="line">szk <span class="number">28</span> <span class="literal">false</span></span><br><span class="line">扫描结果 name:szk age:<span class="number">28</span> married:<span class="literal">false</span></span><br></pre></td></tr></table></figure>

<p><code>fmt.Scan</code>从标准输入中扫描用户输入的数据，将以空白符分隔的数据分别存入指定的参数。</p>
<p>2.<code>fmt.Scanf</code></p>
<blockquote>
<p>Scanf从标准输入扫描文本，根据format参数指定的格式去读取由空白符分隔的值保存到传递给本函数的参数中。</p>
</blockquote>
<p>示例代码：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> (</span><br><span class="line">		name    <span class="keyword">string</span></span><br><span class="line">		age     <span class="keyword">int</span></span><br><span class="line">		married <span class="keyword">bool</span></span><br><span class="line">	)</span><br><span class="line">	fmt.Scanf(<span class="string">"1:%s 2:%d 3:%t"</span>, &amp;name, &amp;age, &amp;married)</span><br><span class="line">	fmt.Printf(<span class="string">"扫描结果 name:%s age:%d married:%t \n"</span>, name, age, married)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>将上面的代码编译后在终端执行，在终端按照指定的格式依次输入<code>szk</code>、<code>28</code>和<code>false</code>。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ ./scan_demo </span><br><span class="line"><span class="number">1</span>:szk <span class="number">2</span>:<span class="number">28</span> <span class="number">3</span>:<span class="literal">false</span></span><br><span class="line">扫描结果 name:szk age:<span class="number">28</span> married:<span class="literal">false</span></span><br></pre></td></tr></table></figure>

<p><code>fmt.Scanf</code>不同于<code>fmt.Scan</code>简单的以空格作为输入数据的分隔符，<code>fmt.Scanf</code>为输入数据指定了具体的输入内容格式，只有按照格式输入数据才会被扫描并存入对应变量。</p>
<p>例如，我们还是按照上个示例中以空格分隔的方式输入，<code>fmt.Scanf</code>就不能正确扫描到输入的数据。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ ./scan_demo </span><br><span class="line">szk <span class="number">28</span> <span class="literal">false</span></span><br><span class="line">扫描结果 name: age:<span class="number">0</span> married:<span class="literal">false</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li><code>fmt.Scanln</code></li>
</ol>
<blockquote>
<p>Scanln类似Scan，它在遇到换行时才停止扫描。最后一个数据后面必须有换行或者到达结束位置</p>
</blockquote>
<p>代码:</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> (</span><br><span class="line">		name    <span class="keyword">string</span></span><br><span class="line">		age     <span class="keyword">int</span></span><br><span class="line">		married <span class="keyword">bool</span></span><br><span class="line">	)</span><br><span class="line">	fmt.Scanln(&amp;name, &amp;age, &amp;married)</span><br><span class="line">	fmt.Printf(<span class="string">"扫描结果 name:%s age:%d married:%t \n"</span>, name, age, married)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>将上面的代码编译后在终端执行，在终端依次输入szk、<code>28</code>和<code>false</code>使用空格分隔。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ ./scan_demo </span><br><span class="line">szk <span class="number">28</span> <span class="literal">false</span></span><br><span class="line">扫描结果 name:szk age:<span class="number">28</span> married:<span class="literal">false</span></span><br></pre></td></tr></table></figure>

<p><code>fmt.Scanln</code>遇到回车就结束扫描了，这个比较常用。</p>

    </div>

  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#Go语言fmt-Printf使用指南"><span class="toc-text">Go语言fmt.Printf使用指南</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#fmt"><span class="toc-text">fmt</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#向外输出的函数"><span class="toc-text">向外输出的函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#格式化占位符"><span class="toc-text">格式化占位符</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#通用占位符"><span class="toc-text">通用占位符</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#获取输入的函数"><span class="toc-text">获取输入的函数</span></a></li></ol></li></ol>
  </div>


  </div>
</div>
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=5sr5du46f27&amp;m=0&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33" async="async"></script>
<div class="copyright">
    <span>本作品采用</span>
    <a href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>


  
    <div class="post-nav" style="margin-left:-168px;">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2019/10/10/五颜六色的发布方式/" rel="next" title="各种部署方式介绍">
          各种部署方式介绍
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2019/10/30/Go语言标准库flag基本使用/" rel="prev" title="GO语言之flag的使用">
            GO语言之flag的使用
          </a>
          <span>〉</span>
        
      </div>
    </div>
  


	
	<div style="width:109%; margin-left:-144px" id="lv-container" data-id="city" data-uid="MTAyMC80OTg5NS8yNjM4Ng==">
	<script type="text/javascript">
   	   (function(d, s) {
       		var j, e = d.getElementsByTagName(s)[0];

       		if (typeof LivereTower === 'function') { return; }

       		j = d.createElement(s);
       		j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
       		j.async = true;

       		e.parentNode.insertBefore(j, e);
   	   })(document, 'script');
	</script>
	<noscript> 为正常使用来必力评论功能请激活JavaScript</noscript>
        </div>
	



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href target="_blank">GitHub</a> |
        <a class="bottom-item" href>友情链接</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/fooying/hexo-theme-xoxo-plus" target="_blank">Theme xoxo-plus</a> |
        <a class="bottom-item" href="/atom.xml">订阅</a>
    </div>
</footer>

  

<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



  

</body>
</html>
